# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=jackett
pkgver=0.21.2458
pkgrel=0
pkgdesc="API Support for your favorite torrent trackers"
url="https://github.com/Jackett/Jackett"
arch="x86_64 aarch64 armv7"
license="GPL-3.0-only"
options="net !check" # no tests
depends="aspnetcore6-runtime"
makedepends="dotnet6-sdk"
subpackages="$pkgname-openrc"
install="$pkgname.pre-install"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/Jackett/Jackett/archive/refs/tags/v$pkgver.tar.gz
	jackett.initd
	jackett.confd
	"
builddir="$srcdir/Jackett-$pkgver/src"
pkgusers="jackett"
pkggroups="jackett"

# map arch to dotnet
case $CARCH in
	x86_64) _dotnet_arch="x64" ;;
	aarch64) _dotnet_arch="arm64" ;;
	armv7) _dotnet_arch="arm" ;;
	*) _dotnet_arch="$CARCH" ;;
esac

# custom variables
_runtime="linux-musl-$_dotnet_arch"
_framework="net6.0"
_output="Jackett.Server/bin/Release"
_artifacts="$_output/$_framework/$_runtime/publish"

prepare() {
	default_prepare

	# replace version info
	local buildprops=Jackett.Common/Jackett.Common.csproj
	sed -i -e "s/<Version>[0-9.*]\+<\/Version>/<Version>$pkgver<\/Version>/g" "$buildprops"
}

build() {
	dotnet publish Jackett.Server \
		-f "$_framework" \
		--no-self-contained \
		-r "$_runtime" \
		-c Release
}

package() {
	local DESTDIR="$pkgdir"/usr/lib/jackett

	mkdir -p "$DESTDIR"
	cp -af "$_artifacts"/* "$DESTDIR"/

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname

	# remove erroneously installed mono runtime library
	rm -rf "$pkgdir"/usr/lib/jackett/libMono.Unix.so
}

sha512sums="
8113b8774a8202b9411b948870b6eab3265ae6a715a68f04dc1445668b7a613e1fe402374a4c4945bbe434d6c6c55128cc8ec3470152d362100dac3c1bf060ae  jackett-0.21.2458.tar.gz
71dc80cee88d0166bcd18a784e9beda0eda6f5b34cf256f65a364375e6407bcf0f6b3eedadc2d1148d48acff9e5489134c28462d92a07c98fb5363e6adf4154a  jackett.initd
c9963ddf11e8ebb35891172226c2486d56f9bafdf7e40c99fab5d34489a5afc37902af55d9cf097d5f9cb713a6bff8b5ce4d04c3d4553b647315ba0d847dbc20  jackett.confd
"
